package com.liu.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Ac {
	private String title, groupId, UUID, uid, eMail, jyUUID, summary, url;
	private Date createTime, updateTime;
	private int status = -99;
	private List<Pwd> pwdLst;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<Pwd> getPwdLst() {
		return pwdLst;
	}

	public void setPwdLst(List<Pwd> pwdLst) {
		this.pwdLst = pwdLst;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getUUID() {
		return UUID;
	}

	public void setUUID(String uUID) {
		UUID = uUID;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String geteMail() {
		if (eMail == null)
			return "";
		return eMail;
	}

	public void seteMail(String eMail) {
		this.eMail = eMail;
	}

	public String getJyUUID() {
		return jyUUID;
	}

	public void setJyUUID(String jyUUID) {
		this.jyUUID = jyUUID;
	}

	public String getSummary() {
		if (summary == null)
			return "";
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public JSONObject toJsonObj() throws JSONException {
		JSONObject obj = new JSONObject();
		obj.put("title", getTitle());
		obj.put("groupId", getGroupId());
		obj.put("UUID", getUUID());
		obj.put("uid", getUid());
		obj.put("eMail", geteMail());
		obj.put("jyUUID", getJyUUID());
		obj.put("summary", getSummary());
		obj.put("url", getUrl());
		obj.put("status", status);
		obj.put("createTime", createTime.getTime());
		obj.put("updateTime", updateTime.getTime());
		if (pwdLst != null) {
			JSONArray ja = new JSONArray();
			for (int i = 0; i < pwdLst.size(); i++) {
				ja.put(pwdLst.get(i).toJsonObj());
			}
			obj.put("pwdLst", ja);
		}
		return obj;
	}

	@SuppressWarnings("unchecked")
	public static Ac JsonToAc(JSONObject obj) throws JSONException {
		Ac ac = new Ac();
		ac.setUUID(obj.getString("UUID"));
		ac.setTitle(obj.getString("title"));
		ac.setGroupId(obj.getString("groupId"));
		ac.setUid(obj.getString("uid"));
		ac.seteMail(obj.getString("eMail"));
		ac.setJyUUID(obj.getString("jyUUID"));
		ac.setSummary(obj.getString("summary"));
		ac.setStatus(obj.getInt("status"));
		if (obj.has("url"))
			ac.setUrl(obj.getString("url"));
		ac.setCreateTime(new Date(obj.getLong("createTime")));
		ac.setUpdateTime(new Date(obj.getLong("updateTime")));
		if (obj.has("pwdLst")) {
			JSONArray ja = obj.getJSONArray("pwdLst");
			List<Pwd> pwdLst = new ArrayList<Pwd>();
			for (int i = 0; i < ja.length(); i++) {
				pwdLst.add(Pwd.JsonToGroup(ja.getJSONObject(i)));
			}
			Collections.sort(pwdLst, new PwdComparator());
			ac.setPwdLst(pwdLst);
		}

		return ac;
	}
}
